package leetcode;

/**
 * Created by Ghost on 2014/12/23 0023.
 */
public class Majority_Element {
    public int majorityElement(int[] num) {
        int history = num[0];
        int n = 1;
        for(int i=1;i<num.length;i++){
            if(num[i]!=history){
                n-=1;
                if(n==0){
                    history = num[i];
                    n = 1;
                }
            }
            else{
                n++;
            }
        }
        return history;
    }

    public static void main(String[] args){
        Majority_Element majority_element = new Majority_Element();
        int[] num = {7,7,7,8,8};
        System.out.println(majority_element.majorityElement(num));
    }
}
